package com.eshopkeeper.service.sys;

import com.baomidou.mybatisplus.plugins.Page;
import com.eshopkeeper.core.base.BaseService;
import com.eshopkeeper.model.sys.SysRole;
import com.eshopkeeper.model.sys.ext.SysMenuBean;
import com.eshopkeeper.provider.sys.ISysRoleProvider;
import com.weibo.api.motan.config.springsupport.annotation.MotanReferer;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenshufeng
 * @Description: 角色
 * @date 2017年3月7日
 */
@Service
public class SysRoleService extends BaseService<ISysRoleProvider, SysRole> {

    @MotanReferer(basicReferer = "clientBasicConfig")
    public void setProvider(ISysRoleProvider provider) {
        this.provider = provider;
    }

    public Page<?> queryBean(Map<String, Object> params) {
        return provider.queryBean(params);
    }

    public List<String> getPermissions(Long id) {
        return provider.getPermissions(id);
    }
    
    @Override
    public void add(SysRole sysRole) {
    	provider.insertSysRole(sysRole);
    }
    
    public void updateEnable(Map<String, Object> paramMap) {
        provider.updateSysRole(paramMap);
	}
    
    public List<SysMenuBean> getMenuList(Long id) {
        return provider.getMenuList(id);
    }
    
    public List<SysMenuBean> getMenuFuncList(Long id) {
        return provider.getMenuFuncList(id);
    }

    public List<Map<String,Object>> selSysRoleList() {
        return provider.selSysRoleList();
    }
    
    @Transactional
    public void updateSysRoleMenu(Map<String, Object> paramMap) throws Exception {
    	Long roleId = Long.parseLong(((String)paramMap.get("roleId")));
        int rs=provider.deleteRoleMenuById(roleId);
        if(rs<0){
        	throw new RuntimeException("角色菜单删除失败！");
        }
        String[] arry = ((String)paramMap.get("menuIds")).split(",");
        List<String> menuIds = new ArrayList<String>();
        for(int i=0;i<arry.length;i++){
        	menuIds.add(arry[i]);
		}
        int rs_=provider.batchAddRoleMenu(menuIds,roleId);
        if(rs_<0){
        	throw new RuntimeException("角色菜单批量新增失败！");
        }
	}
    
    @Transactional
    public void clearSysRoleMenu(Long roleId) throws Exception {
        provider.deleteRoleMenuById(roleId);
	}
    
    @Transactional
    public void updateSysRoleMenuFunc(Map<String, Object> paramMap) throws Exception {
        Long roleId = Long.parseLong(((String)paramMap.get("roleId")));
        int rs=provider.deleteRoleMenuFuncById(roleId);
        if(rs<0){
        	throw new RuntimeException("角色菜单功能删除失败！");
        }
        String[] arry = ((String)paramMap.get("arryFunc")).split(",");
        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
        for(int i=0;i<arry.length;i++){
        	Map<String,Object> map = new HashMap<String, Object>();
        	String[] perArry = arry[i].split("\\|");
        	map.put("menuId", perArry[0]);
        	map.put("permission", perArry[1]);
        	map.put("roleId", (String)paramMap.get("roleId"));
        	list.add(map);
		}
        int rs_=provider.batchAddRoleMenuFunc(list);
        if(rs_<0){
        	throw new RuntimeException("角色菜单功能批量新增失败！");
        }
	}
    
    @Transactional
    public void clearSysRoleMenuFunc(Long roleId) throws Exception {
        provider.deleteRoleMenuFuncById(roleId);
	}
}
